package com.innjia.base.utils.pdf;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;

import com.innjia.base.common.Config;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfUtils {

    /**
     * 创建PDF
     * @param fileName 文件名称
     * @param fileHeader 标题头
     * @param fileContent 内容
     */
    public static String generatePdf(String fileName,String fileHeader,String fileContent){
            Document doc = ItextUtils.createNewPdf("合同", "", "合同");
            PdfWriter writer = null;
            String path = Config.getFileStore() + File.separator + "contract" + File.separator + "temp" + File.separator + fileName+".pdf";
            try {
            	// 默认中文字体
            	BaseFont font = CustomFont.getDefaultFont();
            	// 默认字体大小、样式
            	Font normalFont = new Font(font, 10, Font.NORMAL);
            	//统一段落标题字体
            	Font pHeaderFont = new Font(font,20,Font.BOLD);
            	//统一节标题字体
            	Font pSectionFont = new Font(font,14,Font.BOLD);
            	File file = new File(path);
            	if(!file.getParentFile().exists()) {
            		file.getParentFile().mkdirs();
            		file.createNewFile();
            	}
                writer = PdfWriter.getInstance(doc, new BufferedOutputStream(new FileOutputStream(file)));
                doc.open();
                //文档标题
                ItextUtils.addHeader(doc, fileHeader, 20);
                //添加内容
                doc.add(new Phrase(fileContent,normalFont));
            } catch (FileNotFoundException | DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
				e.printStackTrace();
			} finally {
				if(doc != null ) {
					doc.close();
				}
				if(writer != null) {
					writer.close();
				}
			}
//            添加水印
//            new WaterMarks(directory+fileName+".pdf", "", 120, 45);
            return path;

    }

}
